package cn.ibizlab.odoo.core.odoo_mrp.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imrp_workcenter;
import cn.ibizlab.odoo.core.odoo_mrp.domain.Mrp_workcenter;
import cn.ibizlab.odoo.core.odoo_mrp.filter.Mrp_workcenterSearchContext;
import cn.ibizlab.odoo.core.odoo_mrp.service.IMrp_workcenterService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[工作中心] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mrp_workcenterServiceImpl implements IMrp_workcenterService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imrp_workcenterClientService mrp_workcenterClientService;

    @Override
    public void createBatch(List<Mrp_workcenter> mrp_workcenters, int batchSize) {

                        //mrp_workcenterClientService.createBatch(mrp_workcenters,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mrp_workcenterClientService.removeBatch(mrp_workcenters) ;

    }

    @Override
    public boolean create(Mrp_workcenter mrp_workcenter) {
        boolean bOk = false ;
        Imrp_workcenter clientModel = this.convert2Model(mrp_workcenter,null);
        mrp_workcenterClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mrp_workcenter);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Mrp_workcenter get(Integer id) {
        Imrp_workcenter clientModel = mrp_workcenterClientService.createModel();
        clientModel.setId(id);
        mrp_workcenterClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mrp_workcenter get(Mrp_workcenter mrp_workcenter) {
        Mrp_workcenter retmrp_workcenter = this.get(mrp_workcenter.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmrp_workcenter.getClass(), mrp_workcenter.getClass(), false);
        copier.copy(retmrp_workcenter, mrp_workcenter, null);
        return mrp_workcenter ;
    }

    @Override
    public void updateBatch(List<Mrp_workcenter> mrp_workcenters, int batchSize) {

        //mrp_workcenterClientService.updateBatch(mrp_workcenters,batchSize) ;

    }

    @Override
    public boolean update(Mrp_workcenter mrp_workcenter) {
        boolean bOk = false ;
        Imrp_workcenter clientModel = this.convert2Model(mrp_workcenter,null);
        mrp_workcenterClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mrp_workcenter) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imrp_workcenter clientModel = mrp_workcenterClientService.createModel();
        clientModel.setId(id);
        mrp_workcenterClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mrp_workcenter> searchDefault(Mrp_workcenterSearchContext context) {
        List<Mrp_workcenter> list = new ArrayList<Mrp_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter> clientModelList = mrp_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据资源工作时间查询
     */
    @Override
    public Page<Mrp_workcenter> selectByOdoo_resource_calendar(Integer id){
        Mrp_workcenterSearchContext context = new Mrp_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter> list = new ArrayList<Mrp_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter> clientModelList = mrp_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据资源查询
     */
    @Override
    public Page<Mrp_workcenter> selectByOdoo_resource(Integer id){
        Mrp_workcenterSearchContext context = new Mrp_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter> list = new ArrayList<Mrp_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter> clientModelList = mrp_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Mrp_workcenter> selectByOdoo_company(Integer id){
        Mrp_workcenterSearchContext context = new Mrp_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter> list = new ArrayList<Mrp_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter> clientModelList = mrp_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_workcenter> selectByOdoo_create(Integer id){
        Mrp_workcenterSearchContext context = new Mrp_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter> list = new ArrayList<Mrp_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter> clientModelList = mrp_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_workcenter> selectByOdoo_write(Integer id){
        Mrp_workcenterSearchContext context = new Mrp_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter> list = new ArrayList<Mrp_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter> clientModelList = mrp_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imrp_workcenter convert2Model(Mrp_workcenter domain , Imrp_workcenter model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mrp_workcenterClientService.createModel() ;
        }

         if(domain.getCosts_hourDirtyFlag())
            model.setCosts_hour(domain.getCosts_hour());
         if(domain.getWorkcenter_loadDirtyFlag())
            model.setWorkcenter_load(domain.getWorkcenter_load());
         if(domain.getWorking_stateDirtyFlag())
            model.setWorking_state(domain.getWorking_state());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getWorkorder_late_countDirtyFlag())
            model.setWorkorder_late_count(domain.getWorkorder_late_count());
         if(domain.getPerformanceDirtyFlag())
            model.setPerformance(domain.getPerformance());
         if(domain.getOee_targetDirtyFlag())
            model.setOee_target(domain.getOee_target());
         if(domain.getCapacityDirtyFlag())
            model.setCapacity(domain.getCapacity());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getWorkorder_countDirtyFlag())
            model.setWorkorder_count(domain.getWorkorder_count());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getTime_idsDirtyFlag())
            model.setTime_ids(domain.getTime_ids());
         if(domain.getProductive_timeDirtyFlag())
            model.setProductive_time(domain.getProductive_time());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getRouting_line_idsDirtyFlag())
            model.setRouting_line_ids(domain.getRouting_line_ids());
         if(domain.getOeeDirtyFlag())
            model.setOee(domain.getOee());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getCodeDirtyFlag())
            model.setCode(domain.getCode());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getTime_stopDirtyFlag())
            model.setTime_stop(domain.getTime_stop());
         if(domain.getTime_startDirtyFlag())
            model.setTime_start(domain.getTime_start());
         if(domain.getWorkorder_pending_countDirtyFlag())
            model.setWorkorder_pending_count(domain.getWorkorder_pending_count());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getBlocked_timeDirtyFlag())
            model.setBlocked_time(domain.getBlocked_time());
         if(domain.getWorkorder_ready_countDirtyFlag())
            model.setWorkorder_ready_count(domain.getWorkorder_ready_count());
         if(domain.getWorkorder_progress_countDirtyFlag())
            model.setWorkorder_progress_count(domain.getWorkorder_progress_count());
         if(domain.getOrder_idsDirtyFlag())
            model.setOrder_ids(domain.getOrder_ids());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getTzDirtyFlag())
            model.setTz(domain.getTz());
         if(domain.getTime_efficiencyDirtyFlag())
            model.setTime_efficiency(domain.getTime_efficiency());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getResource_calendar_id_textDirtyFlag())
            model.setResource_calendar_id_text(domain.getResource_calendar_id_text());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getResource_calendar_idDirtyFlag())
            model.setResource_calendar_id(domain.getResource_calendar_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getResource_idDirtyFlag())
            model.setResource_id(domain.getResource_id());
        return model ;
    }

    public Mrp_workcenter convert2Domain( Imrp_workcenter model ,Mrp_workcenter domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mrp_workcenter();
        }

        if(model.getCosts_hourDirtyFlag())
            domain.setCosts_hour(model.getCosts_hour());
        if(model.getWorkcenter_loadDirtyFlag())
            domain.setWorkcenter_load(model.getWorkcenter_load());
        if(model.getWorking_stateDirtyFlag())
            domain.setWorking_state(model.getWorking_state());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getWorkorder_late_countDirtyFlag())
            domain.setWorkorder_late_count(model.getWorkorder_late_count());
        if(model.getPerformanceDirtyFlag())
            domain.setPerformance(model.getPerformance());
        if(model.getOee_targetDirtyFlag())
            domain.setOee_target(model.getOee_target());
        if(model.getCapacityDirtyFlag())
            domain.setCapacity(model.getCapacity());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getWorkorder_countDirtyFlag())
            domain.setWorkorder_count(model.getWorkorder_count());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getTime_idsDirtyFlag())
            domain.setTime_ids(model.getTime_ids());
        if(model.getProductive_timeDirtyFlag())
            domain.setProductive_time(model.getProductive_time());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getRouting_line_idsDirtyFlag())
            domain.setRouting_line_ids(model.getRouting_line_ids());
        if(model.getOeeDirtyFlag())
            domain.setOee(model.getOee());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getCodeDirtyFlag())
            domain.setCode(model.getCode());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getTime_stopDirtyFlag())
            domain.setTime_stop(model.getTime_stop());
        if(model.getTime_startDirtyFlag())
            domain.setTime_start(model.getTime_start());
        if(model.getWorkorder_pending_countDirtyFlag())
            domain.setWorkorder_pending_count(model.getWorkorder_pending_count());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getBlocked_timeDirtyFlag())
            domain.setBlocked_time(model.getBlocked_time());
        if(model.getWorkorder_ready_countDirtyFlag())
            domain.setWorkorder_ready_count(model.getWorkorder_ready_count());
        if(model.getWorkorder_progress_countDirtyFlag())
            domain.setWorkorder_progress_count(model.getWorkorder_progress_count());
        if(model.getOrder_idsDirtyFlag())
            domain.setOrder_ids(model.getOrder_ids());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getTzDirtyFlag())
            domain.setTz(model.getTz());
        if(model.getTime_efficiencyDirtyFlag())
            domain.setTime_efficiency(model.getTime_efficiency());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getResource_calendar_id_textDirtyFlag())
            domain.setResource_calendar_id_text(model.getResource_calendar_id_text());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getResource_calendar_idDirtyFlag())
            domain.setResource_calendar_id(model.getResource_calendar_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getResource_idDirtyFlag())
            domain.setResource_id(model.getResource_id());
        return domain ;
    }


}
